package com.cai.servlet.log;

import com.cai.dao.LogDao;
import com.cai.dao.impl.LogDaoImpl;
import com.cai.entity.LogItem;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;

/**
 * @author RuoLi
 * 物品借出归还
 */
@WebServlet(name = "ServletItemSendAndReturn", value = "/itemSendAndReturn")
public class ServletItemSendAndReturn extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LogItem logItem = new LogItem();
        LogDao logDao = new LogDaoImpl();
        //接收数据
        Map<String, String[]> map = request.getParameterMap();
        //封装数据
        logItem.setUser_name(map.get("user_name")[0]);
        logItem.setUser_email(map.get("user_email")[0]);
        logItem.setUser_type(map.get("user_type")[0]);
        logItem.setItem_name(map.get("item_name")[0]);
        logItem.setLog_sendTime(map.get("send_time")[0]);
        logItem.setLog_send_num(map.get("send_num")[0]);
        logItem.setLog_returnTime(map.get("return_time")[0]);
        logItem.setLog_return_num(map.get("return_num")[0]);
        //获取状态 借出归还
        String status = map.get("item_status")[0];
        String sendStatus = "item_send_status";
        String returnStatus = "item_return_status";
        if (Objects.equals(status, returnStatus)) {
            //归还
            response.getWriter().println(logDao.logReturn(logItem));
        } else if (Objects.equals(status, sendStatus)) {
            //借出
            response.getWriter().println(logDao.logSend(logItem));
        } else {
            //没有点击单选框
            response.getWriter().println(-1);
        }
    }
}
